/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rgenerator;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;

/**
 *
 * @author alex2stf <alex2stf@yahoo.com>
 */
public class FilesGenerator {
    
    //definite de user:
    private String nam = "D:/alex/projects/testAS3";
    private String numePachete="";
    private String mainFileName="undefined";
    private String numeClasa="Class";
    
    private DirectoryReader reader;
    private ArrayList<PackageMaker> pacheteGenerate;
    
    
    private Stringers type=new Stringers();
    
    private FileGenerationEvent event;
    
    
    private String foldere;
    
    public FilesGenerator(Stringers ptype, FileGenerationEvent eventparam){
        this.event=eventparam;
        event.generatingStart();
        
         this.type=ptype;
         this.nam=ptype.inputFolder;
         this.numePachete=ptype.packageName;
         this.mainFileName=ptype.mainClassName;
         
         
         
        //schimb punctul cu slash
         foldere=numePachete.replaceAll("[.]", "/");
         foldere=ptype.outPutFolder+foldere;
         System.out.println(foldere);
        

        try{
        
           boolean success = (new File(foldere)).mkdirs();
            if (success) {
            System.out.println("Directories: " + foldere + " created");
            }
            
        }
        catch(Exception ex){
            System.err.println("Error: " + ex.getMessage());
        }
        
        
        
        
        System.out.println("____________");
        
        
        scrieFisierele();
            
        }//exit constructor
    
    
    
    
    
    
    
    
    
    
    
    public void scrieFisierele(){
        reader=new DirectoryReader(new File(nam), numePachete, mainFileName, numeClasa, type);
        
        pacheteGenerate=reader.getPachete();
        
        
        for (int i = 0; i < pacheteGenerate.size(); i++) {
            try{
             FileWriter outFile = new FileWriter(new File(foldere+"/"+pacheteGenerate.get(i).getFileName()));
             PrintWriter out = new PrintWriter(outFile);
             
                for (int j = 0; j < pacheteGenerate.get(i).getListaDeVariabile().size(); j++) {
                    out.println(pacheteGenerate.get(i).getListaDeVariabile().get(j));
                    System.out.println(pacheteGenerate.get(i).getListaDeVariabile().get(j));
                }
                
                out.close();
             
             
            }
            catch(Exception ex){
                 ex.printStackTrace();
            }
        }
        
        event.generatingStopped();
        
    }
    
    
    
        
   // File aFile = new File(nam);

    
    }
    
    



//        String temp[]=numePachete.split("[.]");//split dupa punct
//       
//        for (int i = 0; i < temp.length; i++) {
//            System.out.println(temp[i]);
//        }


   // 
    
    //pacheteGenerate=reader.getPachete();